package com.hqd.ch03.v28.spel.express;

import com.hqd.ch03.v28.spel.EvaluationContext;
import com.hqd.ch03.v28.spel.Expression;
import org.apache.commons.lang3.ArrayUtils;

import java.util.Arrays;

/**
 * 混合表达式
 */
public class CompositeStringExpression implements Expression {
    private final String expressionString;

    private final Expression[] expressions;


    public CompositeStringExpression(String expressionString, Expression[] expressions) {
        this.expressionString = expressionString;
        this.expressions = expressions;
    }

    @Override
    public String getExpressionString() {
        return expressionString;
    }

    @Override
    public Object getValue() {
        return getValue(null);
    }

    @Override
    public Object getValue(EvaluationContext context) {
        if (ArrayUtils.isEmpty(expressions)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        Arrays.stream(expressions).forEach(expr -> {
            sb.append(expr.getValue(context));
        });
        return sb.toString();
    }

    @Override
    public void setValue(EvaluationContext context, Object value) {

    }
}
